#region 程序集引用
using SQLServer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

#endregion

namespace Zhang.SQLServer.UCenter
{
    /// <summary>
    /// 接口调用日志
    /// </summary>
    public class Notes : BaseMapper, Zhang.IDAL.UCenter.Notes.INotes
    {
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model">数据内容</param>
        /// <returns></returns>
        public int Insert(Zhang.Model.UCenter.Notes model)
        {
            var entity = GetCmdText(model, SqlTypeEnum.Add, "Notes");
            if (string.IsNullOrEmpty(entity.Sql))
            {
                return 0;
            }
            var obj = SqlHelper.ExecuteScalar(CommandType.Text, entity.Sql, entity.LstParam.ToArray());
            return obj.ToInt();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">数据内容</param>
        /// <returns></returns>
        public bool Update(Zhang.Model.UCenter.Notes model)
        {
            var entity = GetCmdText(model, SqlTypeEnum.Update, "Notes");
            if (string.IsNullOrEmpty(entity.Sql))
            {
                return false;
            }
            return SqlHelper.ExecuteNonQuery(CommandType.Text, entity.Sql, entity.LstParam.ToArray()) > 0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>影响行数 </returns>
        public int Delete(int id)
        { 
            var delSql = "DELETE FROM Notes WHERE Id = "+ id + "; ";
            return SqlHelper.ExecuteNonQuery(CommandType.Text, delSql);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="arrPk"></param>
        /// <returns>影响行数 </returns>
        public int DeleteMuch(string[] arrPk)
        {
            var delSql = "DELETE FROM Notes WHERE Id IN(" + string.Join(",", arrPk) + ");";
            return SqlHelper.ExecuteNonQuery(CommandType.Text, delSql);
        }

        /// <summary>
        /// 根据ID查询单个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Zhang.Model.UCenter.Notes Select(int id)
        {
            Zhang.Model.UCenter.Notes model;
            var sql = $"  SELECT * FROM Notes WHERE ID = "+ id;
            using (IDataReader reader = SqlHelper.ExecuteReader(CommandType.Text, sql))
            {
                model = BindData<Zhang.Model.UCenter.Notes>(reader).FirstOrDefault();
            }
            return model;
        }

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <param name="sch">查询参数</param>
        /// <param name="seq">排序字段</param>
        /// <returns></returns>
        public List<Zhang.Model.UCenter.Notes> SelectList(Zhang.Model.UCenter.NotesSch sch, Model.Sequence seq)
        {
            List<Zhang.Model.UCenter.Notes> model;
            var param = GetSqlWhere(sch);
            string orderBy = "ID ASC";
            if (seq != null && !string.IsNullOrEmpty(seq.FieldName))
            {
                orderBy = " " + seq.FieldName + " " + seq.SeqType;
            }
            string sql = $@" SELECT * FROM Notes {param.Where} ORDER BY {orderBy}";
            using (IDataReader reader = SqlHelper.ExecuteReader(CommandType.Text, sql))
            {
                model = BindData<Zhang.Model.UCenter.Notes>(reader);
            }
            return model;
        }

        /// <summary>
        /// 查询集合 - 分页查询
        /// </summary>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页多少条</param>
        /// <param name="sch">查询参数</param>
        /// <param name="seq">排序字段</param>
        /// <returns></returns>
        public (List<Zhang.Model.UCenter.Notes>, int) SelectList(int pageIndex, int pageSize, Zhang.Model.UCenter.NotesSch sch, Zhang.Model.Sequence seq)
        {
            List<Zhang.Model.UCenter.Notes> lstModel;
            var param = GetSqlWhere(sch);
            var orderBy = " ID ASC";
            if (seq != null && !string.IsNullOrEmpty(seq.FieldName))
            {
                orderBy = " " + seq.FieldName + " " + seq.SeqType.ToString();
            }
            var sql = $@"SELECT * From Notes " + param.Where;
            var selSqlData = sql.ToPageSql(orderBy, pageIndex, pageSize);
            var selSqlCount = sql.ToCountSql();
            using (IDataReader dataReader = SqlHelper.ExecuteReader(CommandType.Text, selSqlData, param.LstParam.ToArray()))
            {
                lstModel = BindData<Zhang.Model.UCenter.Notes>(dataReader);
            }
            var totalRs = (int)SqlHelper.ExecuteScalar(CommandType.Text, selSqlCount, param.LstParam.ToArray());
            return (lstModel, totalRs);
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="model">查询条件</param>
        /// <returns></returns>
        private static QueryParam GetSqlWhere(Zhang.Model.UCenter.NotesSch model)
        {
            var param = new QueryParam() { LstParam = new List<SqlParameter>(), Where = string.Empty };
            if (model == null) return param;





            param.Where = string.IsNullOrEmpty(param.Where) ? param.Where : " WHERE " + param.Where;
            return param;
        }

    }
}
